<template>
    <div class="wiper">
        <Filter title="版本管理" :items="filterItems" :model="filterModel"/>
        <Table :columns="columns" :data="data" :pagination="filterModel"/>
        <Form ref="form" v-model:show="show" @uploadSuccess="uploadSuccess" :rules="rules" :model="formModel" :items="formItems">

        </Form>
    </div>
</template>
<script>
    import Filter from "../../components/Filter";
    import Table from "../../components/Table";
    import Form from "../../components/Form";
    import {pager, save, remove, details} from "../../api/version";
    import {ColumnProps, RuleProps, FormProps,
        FilterItems, FormItems, FilterModelProps} from "./config";

    export default {
        name: 'route',
        data(){
            return{
                items: FilterItems,
                data: [],
                show: false,
                formModel: FormProps,
                rules: RuleProps,
                columns: ColumnProps,
                filterModel: {
                    ...FilterModelProps,
                    onChange: (page, pageSize)=>{
                        this.filterModel.current = page;
                        this.filterModel.pageSize = pageSize;
                        this.load();
                    }
                },
                formItems: FormItems
            }
        },
        components:{
            Filter,
            Table,
            Form
        },
        mounted(){
            this.load();
        },
        methods: {

            /**
             * 添加方法
             */
            add(){
                this.formModel.id = "";
                this.$refs.form.resetField();
                this.show = true;
            },

            /**
             * 修改方法
             */
            modify(ids){
                this.show = true;
                details(ids.join(",")).then(res => {
                    this.formModel = res.data;
                })
            },

            /**
             * 删除方法
             */
            delete(ids){
                return new Promise((resolve, reject) => {
                    remove(ids.join(",")).then(res => {
                        this.load();
                        return resolve(res);
                    }).catch(e => {
                        return reject(e);
                    })
                })
            },

            /**
             * 提交
             */
            submit(){
                return new Promise((resolve, reject)=> {
                    save(this.formModel).then(res => {
                        this.load();
                        return resolve(res);
                    }).catch((e)=>{
                        return reject(e)
                    })
                })
            },


            /**
             * 加载数据
             */
            load(){
                pager(this.filterModel).then(res => {
                    this.data = res.data.rows;
                    this.filterModel.total = res.data.total;
                })
            },

            /**
             * 重命名
             * @private
             */
            _rename(list, key = "id"){
                return list?.map(item => {
                    return {label: item.name, value: item[key],children: this._rename(item.children)}
                });
            },

            /**
             * 上传成功
             * @param files 
             */
            uploadSuccess(result){
                this.formModel['appName'] = result.apkInfo.application.label;
                this.formModel['versionName'] = result.apkInfo.versionName;
                this.formModel['versionCode'] = result.apkInfo.versionCode;
                this.formModel['fileName'] = result.files[0].fileName;
                this.formModel['targetFileName'] = result.files[0].targetFileName;
                console.log("上传成功", result);
            }
        }
    }
</script>
<style>

</style>
